package com.owen.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.StyleSet;
import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageInfo;
import com.owen.pojo.Dictionary;
import com.owen.pojo.Interview;
import com.owen.server.DictionaryServer;
import com.owen.server.InterviewServer;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;





import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 字典
 */
@Controller
@RequestMapping(value = "/dictionary")
public class DictionaryController extends BaseController {

    @Autowired
    private DictionaryServer dictionaryServer;

    @RequestMapping(value = "/list")
    public ModelAndView list(Dictionary dictionary,
                             @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                             ModelAndView modelAndView) {
        // 查询数据
        PageInfo<Dictionary> dictionaryPageInfo = dictionaryServer.findByDictionary(dictionary, pageNum, pageSize);
        modelAndView.addObject("data", dictionaryPageInfo);
        modelAndView.addObject("tj", dictionary);
        modelAndView.setViewName("dictionary/list");
        return modelAndView;
    }

    @RequestMapping(value = "/testt")
    public String testt() {
        return "redirect:/dictionary/saveAndEdit";
    }

    /**
     * 详情
     * @param id
     * @param modelAndView
     * @return
     */
    @RequestMapping(value = "/details", method = RequestMethod.GET)
    public ModelAndView details(@RequestParam(name = "id") Integer id,
                                ModelAndView modelAndView) {
        Dictionary dictionary = dictionaryServer.findById(id);
        modelAndView.addObject("dictionary", dictionary);
        modelAndView.setViewName("dictionary/details");
        return modelAndView;
    }

    @RequestMapping(value = "/saveAndEdit", method = RequestMethod.GET)
    public ModelAndView saveAndEdit(@RequestParam(name = "id", defaultValue = "0") Integer id,
                                    ModelAndView modelAndView) {
        if (id != 0) {
            Dictionary dictionary = dictionaryServer.findById(id);
            modelAndView.addObject("dictionary", dictionary);
        }
        modelAndView.setViewName("dictionary/save");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value = "/saveAndEdit", method = RequestMethod.POST)
    public String saveAndEdit(Dictionary dictionary) {
        Map<String, Object> map = new HashMap<>();
        if (dictionary != null) {
            Integer id = dictionary.getId();
            if (id == null) {
                Integer save = dictionaryServer.save(dictionary);
                if (save > 0) {
                    map.put("code", "200");
                    map.put("msg", "添加成功");
                }
            } else {
                Integer update = dictionaryServer.update(dictionary);
                if (update > 0) {
                    map.put("code", "200");
                    map.put("msg", "修改成功");
                }
            }
        }
        return JSON.toJSONString(map);
    }


    @ResponseBody
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.DELETE)
    public String deleteById(@PathVariable(name = "id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        Integer del = dictionaryServer.deleteById(id);
        if (del > 0) {
            map.put("code", "200");
            map.put("msg", "操作成功");
        } else {
            map.put("code", "500");
            map.put("msg", "操作失败");
        }
        return JSON.toJSONString(map);
    }

    @ResponseBody
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
    public String deleteBatch(Integer[] ids) {
        Map<String, Object> map = new HashMap<>();
        Integer del = dictionaryServer.deleteBatch(ids);
        if (del > 0) {
            map.put("code", "200");
            map.put("msg", "操作成功");
        } else {
            map.put("code", "500");
            map.put("msg", "操作失败");
        }
        return JSON.toJSONString(map);
    }


}
